/**
 * Created by cjb on 2016/4/25.
 */

var measureLineLayerId = "measureLayer";
var measureLIneLayer = null;
var measureAreaLayerId = "measureAreaLayer";
var measureAreaLayer = null;

/**
 * 测量距离
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.layers.GraphicsLayer");
 * 	dojo.require("esri.toolbars.draw");
 * 	dojo.require("esri.symbols.SimpleLineSymbol");
 * 	dojo.require("esri.Color");
 * </p>
 */
function measureLine(){
    if(!measureLIneLayer)
        measureLIneLayer = addLayer(measureLineLayerId);
    measureLIneLayer.clear();
    initDraw();
    activeDraw("polyline",drawLineEnd);
}

/**
 * 绘制结束，添加到图层，并测量距离
 * @param graphic 绘制的图标
 */
function drawLineEnd(graphic){
    addGraphic(measureLineLayerId, graphic.geometry,{
        symbol: getSimpleLineSymbol({color: "#ff0000",width:2})
    });
    var pointArray = graphic.geometry.paths[0];
    var endPoint = pointArray[pointArray.length - 1];
    var measureL = measureLength(graphic.geometry);
    addPoint(measureLineLayerId, endPoint[0], endPoint[1], {
        symbol:getTextSymbol(getFormatLength(measureL),{
            fontWeight:esri.symbols.Font.WEIGHT_BOLD,
            fontSize: 14
        })
    });
}

/**
 * 测量距离
 * 使用之前需要加载如下js
 * <p>
 *     dojo.require("esri.geometry.geodesicUtils");
 *     dojo.require("esri.geometry.webMercatorUtils");
 *     dojo.require("esri.units");
 * </p>
 */
function measureLength(geometry) {
    //判断地图投影，若是地理坐标系（经纬度）需转换为投影坐标系（平面坐标系），92xiaoXM坐标系为投影坐标系，无需转换
    if (map.spatialReference.wkid == 102113 || map.spatialReference.wkid == 102100) {
        geometry = esri.geometry.webMercatorUtils.webMercatorToGeographic(geometry);
    }
    else if (map.spatialReference.wkid == 4326) {
        geometry = esri.geometry.webMercatorUtils.webMercatorToGeographic(geometry);
    }
    var lengthsArr = new Array();
    lengthsArr = esri.geometry.geodesicUtils.geodesicLengths([geometry], esri.Units.METERS);
    return Number(lengthsArr[0]);
}

/**
 * 显示测距距离
 */
function getFormatLength(measureLen, unit) {
    if (measureLen == undefined)return "";
    var str = "";
    unit = unit || "默认";
    if(unit == '米'){
            str = Number(measureLen).toFixed(2) + " 米";
    }else if(unit == '千米' || unit == '公里'){
        str = Math.round(measureLen / 10) / 100 + " 千米";
    }else if(unit == '默认'){
        if (measureLen / 1000 > 1) {
            str = Math.round(measureLen / 10) / 100 + " 千米";
        }else {
            str = Number(measureLen).toFixed(2) + " 米";
        }
    }
    return str;
}


/**
 * 测量距离
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.layers.GraphicsLayer");
 * 	dojo.require("esri.toolbars.draw");
 * 	dojo.require("esri.symbols.SimpleFillSymbol");
 * 	dojo.require("esri.symbols.SimpleLineSymbol");
 * 	dojo.require("esri.Color");
 * </p>
 */
function measureArea(){
    if(!measureAreaLayer)
        measureAreaLayer = addLayer(measureAreaLayerId);
    measureAreaLayer.clear();
    initDraw();
    activeDraw("polygon",drawAreaEnd);
}

/**
 * 绘制结束，添加到图层，并测量距离
 * @param graphic 绘制的图标
 */
function drawAreaEnd(graphic){
    addGraphic(measureAreaLayerId, graphic.geometry,{
        symbol:getSimpleFileSymbol({
            outline:getSimpleLineSymbol({color: "#ff0000",width:1}),
            color: [255, 0, 0, 0.5]
        })
    });
    var geometry = graphic.geometry;
    //console.log(JSON.stringify(geometry.toJson()));
    var areaCount = measuringArea(geometry);
    var point = geometry.getExtent().getCenter();
    addPoint(measureAreaLayerId, point.x, point.y, {
        symbol:getTextSymbol(getFormatArea(areaCount),{
            fontWeight:esri.symbols.Font.WEIGHT_BOLD,
            fontSize: 16
        })
    });
}

/**
 * 测量距离
 * 使用之前需要加载如下js
 * <p>
 *     dojo.require("esri.geometry.geodesicUtils");
 *     dojo.require("esri.geometry.webMercatorUtils");
 *     dojo.require("esri.units");
 * </p>
 */
function measuringArea(geometry) {
    if (map.spatialReference.wkid == 102113 || map.spatialReference.wkid == 102100) {
        geometry = esri.geometry.webMercatorUtils.webMercatorToGeographic(geometry);
    }
    else if (map.spatialReference.wkid == 4326) {
        geometry = esri.geometry.webMercatorUtils.webMercatorToGeographic(geometry);
    }
    var lengthsArr = esri.geometry.geodesicUtils.geodesicAreas([geometry], esri.Units.SQUARE_METERS);
    return Number(lengthsArr[0]);
}

/**
 * 显示面积
 */
function getFormatArea(measureArea, unit) {
    if (measureArea == undefined)return "";
    measureArea = Math.abs(measureArea);
    var str = "";
    unit = unit || "默认";
    if(unit == '平方米'){
        str = Number(measureArea).toFixed(2) + " 平方米";
    }else if(unit == '平方千米' || unit == '平方公里'){
        str = (Number(measureArea) / 1000000).toFixed(2) + " 平方千米";
    }else if(unit == '默认'){
        if (measureArea / 1000 > 1) {
            str = (Number(measureArea) / 1000000).toFixed(2) + " 平方千米";
        }else {
            str = Number(measureArea).toFixed(2) + " 平方米";
        }
    }
    return str;
}

/**
 * 清空绘制线的图层
 */
function clearMeasureLineLayer(){
    if(measureLIneLayer)
        measureLIneLayer.clear();
    if(measureAreaLayer)
        measureAreaLayer.clear();
}